package com.emily.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.OPartner;
import com.emily.test.service.PartnerService;
import com.emily.test.service.RedisService;
import com.emily.test.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author ：zc
 * @date ：Created in 2019/4/2 10:24
 * @description：
 */
@Slf4j
@RestController
@RequestMapping(value = "partner")
@Api(value = "合作伙伴", description = "合作伙伴相关接口", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class PartnerController {
    @Autowired
    private PartnerService partnerService;
    @Autowired
    private RedisService redisService;
    @ApiOperation(value="获取合作伙伴列表", notes="根据公司code获取合作伙伴列表")
    @PostMapping(value = "selectPartnerListByCode")
    public ResponseResult selectPartnerListByCode(HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String code= userMap.get("companyCode") == null ? null : (String)userMap.get("companyCode");
        try {
            List<OPartner> list =  partnerService.selectList(code);
            log.info("获取PartnerList={}",list);
            return ResponseResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
    @ApiOperation(value="官网合作伙伴列表", notes="根据公司code获取官合作伙伴")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyCode", value = "companyCode", required = true)
    })
    @PostMapping(value = "getPartnerList")
    public ResponseResult getPartnerList(@RequestBody(required = false) JSONObject jsonObject){
        String code =jsonObject.getString("companyCode");
        try {
            List<OPartner> list =  partnerService.selectList(code);
            log.info("获取PartnerList={}",list);
            return ResponseResult.success("查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
    @ApiOperation(value="增加一个合作伙伴", notes="增加合作伙伴")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgPath", value = "图片地址", required = true),
            @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "partnerName", value = "合作伙伴名称", required = true),
            @ApiImplicitParam(name = "partnerUrl", value = "合作伙伴链接", required = true),
    })
    @PostMapping(value = "insertPartner")
    public ResponseResult insertPartner(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String code= userMap.get("companyCode") == null ? null : (String)userMap.get("companyCode");
        String imgPath = jsonObject.getString("imgPath");
        String partnerName = jsonObject.getString("partnerName");
        String partnerUrl = jsonObject.getString("partnerUrl");
        String id = jsonObject.getString("id");
        try {
            if(id !=null && id.length()!=0 && !id.equals("")){
                log.info("修改合作伙伴");
                OPartner oPartner = partnerService.selectPartner(Integer.parseInt(id));
                oPartner.setImgPath(imgPath);
                oPartner.setCompanyCode(code);
                oPartner.setPartnerName(partnerName);
                oPartner.setPartnerUrl(partnerUrl);
                oPartner.setUpdateTime(DateUtil.getNowStr());
                partnerService.updeae(oPartner);
            }else {
                log.info("新建合作伙伴");
                OPartner o = new OPartner();
                o.setImgPath(imgPath);
                o.setCompanyCode(code);
                o.setPartnerName(partnerName);
                o.setPartnerUrl(partnerUrl);
                o.setUpdateTime(DateUtil.getNowStr());
                partnerService.insert(o);
            }
            log.info("增加成功");
            return ResponseResult.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("增加失败");
        }
    }

}
